package com.tb.dubbo_service;

import com.tb.dubbo.IGoodsService;
import com.tb.mapper.GoodsMapper;
import com.tb.mapper.ImgMapper;
import com.tb.pojo.Goods;
import com.tb.pojo.Img;
import com.tb.mapper.NatureMapper;
import com.tb.mapper.NatureValueMapper;
import com.tb.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class GoodsServiceImpl implements IGoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ImgMapper imgMapper;

    @Autowired
    private NatureMapper natureMapper;

    @Autowired
    private NatureValueMapper natureValueMapper;

    @Override
    public Integer deleteGoods(String goodsId) {
        int num =goodsMapper.deleteByPrimaryKey( goodsId );
        imgMapper.deleteByGoodsId(goodsId);
        return num;

    }

    @Override
    public boolean updateGoods(Goods2 goods2) {
        String  numId= goods2.getGoodsId();
        int goodnum=0;//商品修改改变行数
        int imgnum=0;//图片修改改变行数
        int naturenum=0;//属性修改改变行数
        int naturevaluenum=0;//属性键值对改变行数
        Goods goods =new Goods( goods2.getGoodsId(),goods2.getGoodsName(),goods2.getGoodsPrice(),goods2.getGoodsKind(),goods2.getGoodsWeight(),goods2.getGoodsDiscount() );
        goods.setGoodsNum( goods2.getGoodsNum() );
        //修改Goods表
         goodnum = goodsMapper.updateByPrimaryKeySelective( goods );

        //取出所有该商品的图片
        List<Img> imgs = goods2.getImg();
        if(imgs!=null){
            for(Img i : imgs){
                Img img = new Img();
                img.setGoodsId( numId );
                img.setImgUrl( i.getImgUrl() );
                img.setId( i.getId() );

                //将每一个图片存到数据库中
                imgnum =  imgMapper.updateByPrimaryKeySelective( img );
            }
        }

        List<Nature> natures = goods2.getNatures();
        //取出Nature中的所有key-value
        if(natures!=null) {
            for (Nature nature : natures) {
                //拿到属性Id更改属性值
                Nature nature1 = new Nature();
                nature1.setNature( nature.getNature() );
                nature1.setGoodsId( numId );
                nature1.setId( nature.getId() );
                //修改nature表
                naturenum = natureMapper.updateByPrimaryKeySelective( nature1 );


                //拿到属性中的中的参数
                List<NatureValue> naturev = nature.getNaturev();
                if(naturev!=null) {
                    for (NatureValue natureValue : naturev) {
                        NatureValue natureValue1 = new NatureValue();
                        Map<String, String> map = natureValue.getMap();
                        //下面的for循环只有一次
                        for (Map.Entry<String, String> entry : map.entrySet()) {
                            natureValue1.setId( natureValue.getId() );
                            natureValue1.setNatureKey( entry.getKey() );
                            natureValue1.setNatureValue( entry.getValue() );
                            System.out.println( natureValue.getId() + "=================" );
                            //修改natureValue表
                            naturevaluenum = natureValueMapper.updateByPrimaryKeySelective( natureValue1 );
                        }
                    }
                }
                if (goodnum == 0 && imgnum == 0 && naturenum == 0 && naturevaluenum == 0) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public Goods goodsDetails(Map<String, String> map) {
        return goodsMapper.goodsDetails(map);
    }

    @Override
    public List<Img> selectPic(Map<String, String> map) {
        return imgMapper.selectPic(map);
    }

    @Override
    public String getId(String goodsName) {
        String goodsId = goodsMapper.getId(goodsName);
        return goodsId;
    }

    @Override
    public List<ShopParam> getGoodsParam(Map<String, String> map) {
        return goodsMapper.getGoodsParam(map);
    }
}
